<template>
  <div class="app-container">
    <div id="lottie-container"></div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="礼物名" prop="giftName">
        <el-input
          v-model="queryParams.giftName"
          placeholder="请输入礼物名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:gift:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="giftList">
      <el-table-column label="序号" type="index" align="center" width="80"> </el-table-column>
      <el-table-column label="直播礼物编号" align="center" prop="id" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="礼物名" align="center" prop="giftName" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="礼物图标" align="center" prop="giftIcon" width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <oss-image-preview v-if="scope.row.giftIcon" :src="scope.row.giftIcon" :width="30" :height="30"/>
        </template>
      </el-table-column>
      <el-table-column label="金币单价" align="center" prop="giftGold" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="上/下架" align="center" prop="status" width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
<!--          <dict-tag :options="dict.type.gift_status" :value="scope.row.status" />-->
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0" @change="statusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <div>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改直播礼物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="礼物名" prop="giftName">
          <el-input v-model="form.giftName" placeholder="请输入礼物名" />
        </el-form-item>
        <el-form-item label="礼物图标" prop="giftIcon">
<!--          <el-input v-model="form.giftIcon" placeholder="请输入礼物图标" />-->
          <ImageUpload :limit="1" v-model="form.giftIcon"></ImageUpload>
        </el-form-item>
        <el-form-item label="礼物特效" prop="giftStyle">
          <ossUploadSvga :limit="1" v-model="form.giftStyle"></ossUploadSvga>
        </el-form-item>
        <el-form-item label="金币单价" prop="giftGold">
          <el-input type="number" :min="0" v-model="form.giftGold" placeholder="请输入金币单价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGift, getGift, delGift, addGift, updateGift,changeStatus } from "@/api/live/gift";
import OssImagePreview from '@/components/ImagePreview/oss-image-preview.vue'

import lottie from 'lottie-web';
import lottieConfig from 'lottie-web/build/player/lottie_svg';
export default {
  name: "Gift",
  components: { OssImagePreview },
  dicts:['gift_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 直播礼物表格数据
      giftList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        giftName: [
          { required: true, message: "礼物名不能为空", trigger: "blur" }
        ],
        giftIcon: [
          { required: true, message: "礼物图标不能为空", trigger: "blur" }
        ],
        giftGold: [
          { required: true, message: "金币单价不能为空", trigger: "blur" }
        ],
        giftStyle: [
          { required: true, message: "礼物特效不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  // 用来展示svga
  mounted() {
    const container = document.getElementById('lottie-container');
    const animation = lottie.loadAnimation({
      container: container,
      path: 'https://live-mall-1319900789.cos.ap-singapore.myqcloud.com/sys_other/2023-08-18/16923431586688265.svga', // 替换为你的SVGA文件路径
      renderer: 'svg',
      loop: true,
      autoplay: true,
      ...lottieConfig // 引入配置
    });
  },
  methods: {
    /** 查询直播礼物列表 */
    getList() {
      this.loading = true;
      listGift(this.queryParams).then(response => {
        this.giftList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.form.clearValidate();
    },
    // 表单重置
    reset() {
      this.form = {
        giftName: null,
        giftIcon: null,
        giftGold: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加直播礼物";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGift(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改直播礼物";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('this.form',this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGift(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGift(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除直播礼物编号为"' + ids + '"的数据项？').then(function() {
        return delGift(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/gift/export', {
        ...this.queryParams
      }, `gift_${new Date().getTime()}.xlsx`)
    },
    /** 修改状态 */
    statusChange(row) {
      changeStatus(row.id).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
  }
};
</script>
